package cn.lyn4ever.user.controller;

import cn.lyn4ever.common.R;
import cn.lyn4ever.entity.UserAddress;
import cn.lyn4ever.user.service.UserAddressService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 微信公众号 “小鱼与Java”
 *
 * @date 2020/4/12
 * @auther Lyn4ever
 */
@Resource
@RequestMapping("address")
@RestController
@Api(value = "用户地址相关的api")
public class UserAddressController {


    @Resource
    private UserAddressService userAddressService;


    @ApiOperation(value = "获取保存的全部地址信息")
    @GetMapping("list/{userid}")
    public R getAddressList(@PathVariable("userid") String userid) {
        QueryWrapper<UserAddress> wrapper = new QueryWrapper<UserAddress>();
        wrapper.eq("userid", userid);
        wrapper.orderByDesc("update_time");

        List<UserAddress> list = userAddressService.list(wrapper);

        if (list.size()>0) {
            return R.ok().put("data", list);
        }
        return R.error("没有地址");
    }

    @ApiOperation(value = "新增一条地址信息")
    @PostMapping("insertOne")
    public R insertNewOne(@ApiParam @RequestBody UserAddress userAddress) {

        userAddress.setUpdateTime(new Date());
        boolean flag = userAddressService.save(userAddress);
        if (flag) {
            return R.ok();
        }
        return R.error("地址添加失败");
    }

}
